﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

using FlickrPortfolio.Helpers;

using FlickrNet;

namespace FlickrPortfolio.DataAccess {
    public class Flickr {

        private readonly FlickrNet.Flickr flickr;

        public Flickr() {
            //if (AppHelper.GetToken().IsNullOrEmpty())
            //    throw new Exception("Flickr authorisation has not been completed.");

            flickr = new FlickrNet.Flickr(AppHelper.GetAPIKey(), AppHelper.GetAPISecret(), AppHelper.GetToken());
        }

        public IList<Photo> GetPhotos() {
            var options = new PhotoSearchOptions {
                                                     Extras = PhotoSearchExtras.MachineTags,
                                                     PerPage = 500,
                                                     UserId = "me"
                                                 };

            var photos = flickr.PhotosSearch(options).PhotoCollection;
            return photos;
        }

        public PhotoInfo GetPhoto(string id) {
            var photo = flickr.PhotosGetInfo(id);
            return photo;
        }

        public Photoset GetAlbum(string id) {
            var album = flickr.PhotosetsGetInfo(id);
            return album;
        }

        public IList<Photoset> GetAlbums() {
            var albums = flickr.PhotosetsGetList(AppHelper.GetUserID()).PhotosetCollection;
            return albums;
        }

        public IList<Photo> GetPhotosByTag(string raw) {
            var options = new PhotoSearchOptions {
                                                 Extras = PhotoSearchExtras.MachineTags,
                                                 PerPage = 500,
                                                 Tags = raw,
                                                 UserId = "me"
                                             };

            var photos = flickr.PhotosSearch(options).PhotoCollection;
            return photos;
        }

        public IList<Tag> GetTags() {
            var tags = flickr.TagsGetListUserPopular(AppHelper.GetUserID());
            return tags;
        }

        public Auth GetToken(string miniToken) {
            var token = flickr.AuthGetFullToken(miniToken);
            flickr.AuthToken = token.Token;
            return token;
        }
    }
}
